home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zmdm.zoo / tyme.c < prev    next >
C/C++ Source or Header  |  1991-04-27  |  5KB  |  222 lines

  1. /*
  2.  *     Time conversions Module
  3.  *
  4.  *    Jwahar Bammi
  5.  *     bang:   {any internet host}!dsrgsun.ces.CWRU.edu!bammi
  6.  *     domain: bammi@dsrgsun.ces.CWRU.edu
  7.  *    GEnie:    J.Bammi
  8.  */
  9.  
  10.  
  11. #include "config.h"
  12. #include <stdio.h>
  13.  
  14. #if defined(__STDC__) || defined(__cplusplus)
  15. # define P_(s) s
  16. #else
  17. # define P_(s) ()
  18. #endif
  19.  
  20. static int leap P_((int y));
  21. static unsigned long ndays P_((unsigned int since, unsigned int year, unsigned int month, unsigned int day));
  22.  
  23. #undef P_
  24.  
  25. /*
  26.  * days in a given year
  27.  */
  28. #define days_in_year(Y) (leap(Y) ? 366 : 365)
  29.  
  30. /* # of days / month in a normal year */
  31. static unsigned int md[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  32.  
  33. static int leap (y)
  34. int y;
  35. {
  36.     y += 1900;
  37.     if ((y % 400) == 0)
  38.         return (1);
  39.     if ((y % 100) == 0)
  40.         return (0);
  41.     return ((y % 4) == 0);
  42. }
  43.  
  44. /* Return the number of days between Jan 1, Given Year and the given
  45.  * broken-down time.
  46.  */
  47.  
  48. static unsigned long ndays (since, year, month, day)
  49. unsigned int since, year, month, day;
  50. {
  51.     register unsigned long n = day;
  52.     register unsigned int m, y;
  53.     
  54.     for (y = since; y < year; y++)
  55.     {
  56.         n += 365;
  57.         if (leap (y)) n++;
  58.     }
  59.     if(month > 0)
  60.         for (m = 0; m < (month-1); m++)
  61.             n += md[m] + ( ((m == 1) && leap(y))? 1 : 0);
  62.  
  63.     return (n);
  64. }
  65.  
  66. /* Convert a broken-down time into seconds
  67.  *
  68.  */
  69.  
  70. unsigned long tm_to_time (base_year, year, month, day, hours, mins, secs)
  71. unsigned int base_year, year, month, day, hours, mins, secs;
  72. {
  73.     register unsigned long t;
  74.     extern unsigned long ndays();
  75.     
  76.     t = (ndays(base_year, year, month, day) - 1L) * (unsigned long)86400L
  77.         + hours * (unsigned long)3600L + mins * (unsigned long)60L + secs;
  78.  
  79.     return t;
  80. }
  81.  
  82. /*
  83.  * Convert ST time to Unix time
  84.  *
  85.  */
  86. unsigned long st2unix(time, date)
  87. unsigned int time, date;
  88. {
  89.     extern unsigned long tm_to_time();
  90.     
  91.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  92.     unsigned int mo = (date >> 5) & 0x000f;
  93.     unsigned int dy = date & 0x1f;
  94.     
  95.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  96.     unsigned int mm = (time >> 5)  & 0x003f;
  97.     unsigned int ss = (time & 0x001f) * 2;
  98.  
  99. #ifdef SDEBUG
  100.     printf("%d/%d/%d  %d:%d:%d\n", mo, dy, yr,hr, mm, ss);
  101. #endif
  102.     return (tm_to_time(70, yr, mo, dy, hr, mm, ss) -
  103.         (unsigned long)GMTDIFF);
  104. }
  105.  
  106. /*
  107.  * Convert Unix Time to ST Time
  108.  *
  109.  */
  110. void unix2st(Unix, time, date)
  111. unsigned long Unix;
  112. unsigned int *date, *time;
  113. {
  114.     long stbase;
  115.     unsigned int hours, yrs, day, months, mins, seconds, t;
  116.     long days, secs;
  117.     extern unsigned long tm_to_time();
  118.  
  119. #ifdef DEBUG
  120. printf("\n\nUnix Time %ld\n", Unix);
  121. #endif
  122.  
  123. #if 0
  124.     if((Unix - tm_to_time(70, 80, 0, 0, 0, 0, 0)) <= 0)  /* base 1980 */
  125. #else
  126.     if((Unix - 0x12dc5480) <= 0)  /* base 1980 */
  127. #endif
  128.     {
  129.         /* thats before St's time */
  130.         *time = 0;
  131.         *date = (1 << 5) | 1;    /* Jan 1, 1980 00:00:00 GMT */
  132. #ifdef DEBUG
  133. printf("Before my time\n");
  134. #endif
  135.         return;
  136.     }
  137.  
  138.     stbase = Unix;    /* do from base year 1970 */
  139.  
  140.     days = stbase / 86400L;    /* 3600*24 */
  141.     secs = stbase % 86400L + GMTDIFF;
  142.     if(secs < 0)    /* previous day here */
  143.     {
  144.         days -= 1;
  145.         secs += 86400L;
  146.     }
  147.  
  148.     /* extract hrs : mins : seconds */
  149.     hours = secs / 3600;
  150.     secs = secs - (hours * 3600);
  151.     mins = secs / 60;
  152.     seconds = secs - (mins * 60);
  153.     seconds &= ~1L;            /* ST has 2 sec resolution */
  154.  
  155.     /* get the year and day of the year */
  156.     for(t = 70; days >= days_in_year(t); t++)
  157.         days -= days_in_year(t);
  158.     yrs = t;
  159.     day = days;
  160.  
  161.     /* get the month */
  162.     if(days_in_year(yrs) == 366)
  163.         md[1] = 29;
  164.  
  165.     for(t = 0; day >= md[t]; t++)
  166.         day -= md[t];
  167.  
  168.     md[1] = 28;        
  169.     day = day + 1;
  170.     months = t + 1;
  171.  
  172. #ifdef DEBUG
  173. printf("%d/%d/%d   %d:%d:%d\n", months, day, yrs, hours, mins, seconds);
  174. #endif
  175.  
  176.     yrs -= 80;
  177.     *date = (((yrs & 0x007f) << 9) | ((months & 0x000f) << 5)
  178.           | (day & 0x001f));
  179.     
  180.     *time = (((hours & 0x001f) << 11) | ((mins & 0x003f) << 5)
  181.           | (seconds & 0x001e));
  182. }
  183.  
  184. #ifdef TEST
  185. #include <stdio.h>
  186. #include <osbind.h>
  187.  
  188. main()
  189. {
  190.     unsigned int time, date;
  191.     unsigned long Unix;
  192.     extern unsigned long st2unix();
  193.     
  194.     time = Tgettime();
  195.     date = Tgetdate();
  196.     Unix = st2unix(time, date);
  197.  
  198.     printd(time, date);
  199.     printf("Unix Time %ld\n", Unix);
  200.  
  201.     unix2st(Unix, &time, &date);
  202.     printd(time, date);
  203. }
  204.  
  205. printd(time, date)
  206. unsigned int time, date;
  207. {
  208.     
  209.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  210.     unsigned int mo = (date >> 5) & 0x000f;
  211.     unsigned int dy = date & 0x1f;
  212.     
  213.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  214.     unsigned int mm = (time >> 5)  & 0x003f;
  215.     unsigned int ss = (time & 0x001f) * 2;
  216.  
  217.     printf("%d/%d/%d\t%d:%d:%d\n", mo, dy, yr, hr, mm, ss);
  218. }
  219. #endif /* TEST */
  220.  
  221. /* -eof- */
  222.